{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDFile(dir):\n",
    "    files = os.listdir(dir)\n",
    "    imgs = []\n",
    "    labels = []\n",
    "    for file_name in files:\n",
    "        imgs.append(''.join(np.loadtxt(f'{dir}/{file_name}',dtype=str)))\n",
    "        labels.append(file_name.split('_')[0])\n",
    "    return pd.DataFrame({\n",
    "        \"imgs\":imgs,\n",
    "        \"labels\":labels\n",
    "    })"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDFiles(dirs):\n",
    "    return list(map(getDFile,dirs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only join an iterable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\Administrator\\azha\\2-3.ipynb Cell 4'\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000003?line=0'>1</a>\u001b[0m [text,train]\u001b[39m=\u001b[39mgetDFiles([\u001b[39m\"\u001b[39;49m\u001b[39mdigits/testDigits\u001b[39;49m\u001b[39m\"\u001b[39;49m,\u001b[39m\"\u001b[39;49m\u001b[39mdigits/trainingDigits\u001b[39;49m\u001b[39m\"\u001b[39;49m])\n",
      "\u001b[1;32mc:\\Users\\Administrator\\azha\\2-3.ipynb Cell 3'\u001b[0m in \u001b[0;36mgetDFiles\u001b[1;34m(dirs)\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000002?line=0'>1</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mgetDFiles\u001b[39m(dirs):\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000002?line=1'>2</a>\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mlist\u001b[39;49m(\u001b[39mmap\u001b[39;49m(getDFile,dirs))\n",
      "\u001b[1;32mc:\\Users\\Administrator\\azha\\2-3.ipynb Cell 2'\u001b[0m in \u001b[0;36mgetDFile\u001b[1;34m(dir)\u001b[0m\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=3'>4</a>\u001b[0m labels \u001b[39m=\u001b[39m []\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=4'>5</a>\u001b[0m \u001b[39mfor\u001b[39;00m file_name \u001b[39min\u001b[39;00m files:\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=5'>6</a>\u001b[0m     imgs\u001b[39m.\u001b[39mappend(\u001b[39m'\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m.\u001b[39;49mjoin(np\u001b[39m.\u001b[39;49mloadtxt(\u001b[39mf\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m{\u001b[39;49;00m\u001b[39mdir\u001b[39;49m\u001b[39m}\u001b[39;49;00m\u001b[39m/\u001b[39;49m\u001b[39m{\u001b[39;49;00mfile_name\u001b[39m}\u001b[39;49;00m\u001b[39m'\u001b[39;49m,dtype\u001b[39m=\u001b[39;49m\u001b[39mstr\u001b[39;49m)))\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=6'>7</a>\u001b[0m     labels\u001b[39m.\u001b[39mappend(file_name\u001b[39m.\u001b[39msplit(\u001b[39m'\u001b[39m\u001b[39m_\u001b[39m\u001b[39m'\u001b[39m)[\u001b[39m0\u001b[39m])\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=7'>8</a>\u001b[0m \u001b[39mreturn\u001b[39;00m pd\u001b[39m.\u001b[39mDataFrame({\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=8'>9</a>\u001b[0m     \u001b[39m\"\u001b[39m\u001b[39mimgs\u001b[39m\u001b[39m\"\u001b[39m:imgs,\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=9'>10</a>\u001b[0m     \u001b[39m\"\u001b[39m\u001b[39mlabels\u001b[39m\u001b[39m\"\u001b[39m:labels\n\u001b[0;32m     <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000001?line=10'>11</a>\u001b[0m })\n",
      "\u001b[1;31mTypeError\u001b[0m: can only join an iterable"
     ]
    }
   ],
   "source": [
    "[text,train]=getDFiles([\"digits/testDigits\",\"digits/trainingDigits\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'train' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\Administrator\\azha\\2-3.ipynb Cell 5'\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=0'>1</a>\u001b[0m dist1\u001b[39m=\u001b[39mtrain\u001b[39m.\u001b[39miloc[:,\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39mapply(\u001b[39mlambda\u001b[39;00m x: hanm(A,x))\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=2'>3</a>\u001b[0m dist_s\u001b[39m=\u001b[39m((pd\u001b[39m.\u001b[39mDataFrame({\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=3'>4</a>\u001b[0m     \u001b[39m'\u001b[39m\u001b[39mdist\u001b[39m\u001b[39m'\u001b[39m:dist1,\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=4'>5</a>\u001b[0m     \u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m:train\u001b[39m.\u001b[39miloc[:,\u001b[39m-\u001b[39m\u001b[39m1\u001b[39m]\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=5'>6</a>\u001b[0m }))\u001b[39m.\u001b[39msort_values(by\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mdist\u001b[39m\u001b[39m\"\u001b[39m))\u001b[39m.\u001b[39miloc[:\u001b[39m5\u001b[39m]\n\u001b[0;32m      <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000004?line=7'>8</a>\u001b[0m dist_s\u001b[39m.\u001b[39mvalue_counts(\u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m)\u001b[39m.\u001b[39mindex[\u001b[39m0\u001b[39m]\n",
      "\u001b[1;31mNameError\u001b[0m: name 'train' is not defined"
     ]
    }
   ],
   "source": [
    "dist1=train.iloc[:,0].apply(lambda x: hanm(A,x))\n",
    "\n",
    "dist_s=((pd.DataFrame({\n",
    "    'dist':dist1,\n",
    "    'label':train.iloc[:,-1]\n",
    "})).sort_values(by=\"dist\")).iloc[:5]\n",
    "\n",
    "dist_s.value_counts('label').index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def knn(inX,df,k):\n",
    "    dist = df.iloc[:,0].apply(lambda x: hanm(inX,x))\n",
    "    dist_1 = pd.DataFrame({\n",
    "        'dist':dist1,\n",
    "    'label':train.iloc[:,-1]\n",
    "    })\n",
    "    dist_k = (dist_1.sort_values(by=\"dist\")).iloc[:k]\n",
    "    return dist_k.value_counts('label').index[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def digitsTest(test,train,k):\n",
    "    predict =[]\n",
    "    for _,row in test.iterrows():\n",
    "        predict.append(knn(row[0],train,k))\n",
    "    \n",
    "    return np.mean(predict == test.iloc[:,-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'B' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\Users\\Administrator\\azha\\2-3.ipynb Cell 8'\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/Administrator/azha/2-3.ipynb#ch0000007?line=0'>1</a>\u001b[0m knn(B,text,\u001b[39m3\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'B' is not defined"
     ]
    }
   ],
   "source": [
    "knn(B,text,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(os.listdir(\"./testDigits\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def digitsTest(test,train,k):\n",
    "    return acc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.loadtxt('./testDigits/0_0.txt',dtype=str)\n",
    "''.join(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "getDFile(\"./testDigits\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def digitsTest(text,train,k):\n",
    "    predict =[]\n",
    "    for _,row in text.iterrows():\n",
    "        predict.append(knn(row[0],train,k))\n",
    "    \n",
    "    return np.mean(predict == text.iloc[:,-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "digitsTest(text,train,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [1,2,3,4]\n",
    "\n",
    "B = [1,2,3,4]\n",
    "\n",
    "def  hanm(A,B):\n",
    "     return sum([a==b for (a,b) in zip(A,B)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hanm(text.iloc[0,0],text.iloc[50,0])"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a6dc62afd8b03c17538a9dfce2fcb18f62cec380cc7b77050462a64b7e4e4814"
  },
  "kernelspec": {
   "display_name": "Python 3.8.0 32-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
